package com.zbp.web;

import com.zbp.config.exception.Result;
import com.zbp.utils.RedisUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.Authentication;
import org.springframework.security.core.context.SecurityContext;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@Slf4j
@RestController
@RequestMapping(value = "/redis")
@Api(tags = "Redis测试")
public class RedisController {

    @Autowired
    private RedisUtil redisUtil;

    @GetMapping(value = "/{id}")
    @ApiOperation(value = "根据ID获取用户信息")
    public Result<String> get(@PathVariable String id) {
        return Result.success(redisUtil.get(id));
    }

    @GetMapping("/hello")
    public Result<Object> hello() {
        SecurityContext context = SecurityContextHolder.getContext();
        Authentication authentication = context.getAuthentication();
        return Result.success(authentication.getPrincipal());
    }
}
